//
//  ShipVechilesDetailViewController.m
//  WHB_cargo
//
//  Created by 徐国庆 on 15/12/15.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "ShipVechilesDetailViewController.h"
#import "CWStarRateView.h"

@interface ShipVechilesDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;

@end

@implementation ShipVechilesDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitle.text = @"货车详情";
    [self initWithView];
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"shipVechilesDetailCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    // 车号
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-30, 0, kUIScreenWidth / 2, 30)];
    [view addSubview:[self titleAndContent:@"车号:" andContent:@"京A12345" withToAddress:nil]];
    [cell.contentView addSubview:view];
    
    // 姓名
    UIView *nameView = [[UIView alloc] initWithFrame:CGRectMake(kUIScreenWidth / 2  - 30, 0, kUIScreenWidth / 2, 30)];
    [nameView addSubview:[self titleAndContent:@"姓名:" andContent:@"王晓辉" withToAddress:nil]];
    [cell.contentView addSubview:nameView];
    
    // 承运商
    UIView *forwardingAgentView = [[UIView alloc] initWithFrame:CGRectMake(-30, view.frame.origin.y + view.frame.size.height, kUIScreenWidth - 50, 30)];
    [forwardingAgentView addSubview:[self titleAndContent:@"承运商:" andContent:@"承运商全称" withToAddress:nil]];
    [cell.contentView addSubview:forwardingAgentView];
    
    // 是否认证图标
    UIImageView *indentiferImageView = [[UIImageView alloc] initWithFrame:CGRectMake(forwardingAgentView.frame.origin.x + forwardingAgentView.frame.size.width, view.frame.origin.y + view.frame.size.height + 10, 60, 20)];
    indentiferImageView.image = [UIImage imageNamed:@"yrz"];
    [cell.contentView addSubview:indentiferImageView];
    
    // 服务评价
    UIView *serveEvaluateView = [[UIView alloc] initWithFrame:CGRectMake(-20, forwardingAgentView.frame.origin.y + forwardingAgentView.frame.size.height, kUIScreenWidth, 30)];
    [serveEvaluateView addSubview:[self fiveCellView:@"服务评价:" withStar:2]];
    [cell.contentView addSubview:serveEvaluateView];
    
    // 服务次数
    UIView *serveCountView = [[UIView alloc] initWithFrame:CGRectMake(-20, serveEvaluateView.frame.origin.y + serveEvaluateView.frame.size.height, kUIScreenWidth - 50, 30)];
    [serveCountView addSubview:[self titleAndContent:@"服务次数:" andContent:@"12" withToAddress:nil]];
    [cell.contentView addSubview:serveCountView];
    
    // 服务次数下的阴影线
    UILabel *serveCountBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, serveCountView.frame.origin.y + serveCountView.frame.size.height + 10, kUIScreenWidth, 15)];
    serveCountBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"FCFCFC"];
    serveCountBottomLineLabel.layer.borderWidth = 0.5;
    serveCountBottomLineLabel.layer.borderColor = [UIColor HexStrWithColor:@"d0d0d0"].CGColor;
    [cell.contentView addSubview:serveCountBottomLineLabel];
    
    // 路线
    UIView *roadLineView = [[UIView alloc] initWithFrame:CGRectMake(-20, serveCountBottomLineLabel.frame.origin.y + serveCountBottomLineLabel.frame.size.height, kUIScreenWidth - 50, 30)];
    [roadLineView addSubview:[self titleAndContent:@"路线:" andContent:@"深圳" withToAddress:@"上海"]];
    [cell.contentView addSubview:roadLineView];
    
    // 位置
    UIView *addressView = [[UIView alloc] initWithFrame:CGRectMake(-20, roadLineView.frame.origin.y + roadLineView.frame.size.height, kUIScreenWidth - 50, 30)];
    [addressView addSubview:[self titleAndContent:@"位置:" andContent:@"哈尔滨南岗区西大值街318号" withToAddress:nil]];
    [cell.contentView addSubview:addressView];
    
    // 距离
    UIView *distanceView = [[UIView alloc] initWithFrame:CGRectMake(-20, addressView.frame.origin.y + addressView.frame.size.height, kUIScreenWidth - 50, 30)];
    [distanceView addSubview:[self titleAndContent:@"距离:" andContent:@"1公里" withToAddress:nil]];
    [cell.contentView addSubview:distanceView];
    
    // 距离次数下的阴影线
    UILabel *distanceBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, distanceView.frame.origin.y + distanceView.frame.size.height + 10, kUIScreenWidth, 15)];
    distanceBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"FCFCFC"];
    distanceBottomLineLabel.layer.borderWidth = 0.5;
    distanceBottomLineLabel.layer.borderColor = [UIColor HexStrWithColor:@"d0d0d0"].CGColor;
    [cell.contentView addSubview:distanceBottomLineLabel];
    
    
    // 车辆品牌前面的图片
    UIImageView *frontImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, distanceBottomLineLabel.frame.origin.y + distanceBottomLineLabel.frame.size.height + 20, 100, 100)];
    frontImageView.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:frontImageView];
    
    // 车辆品牌
    UIView *vechileBrandView = [[UIView alloc] initWithFrame:CGRectMake(frontImageView.frame.origin.x + frontImageView.frame.size.width - 10, distanceBottomLineLabel.frame.origin.y + distanceBottomLineLabel.frame.size.height, kUIScreenWidth - frontImageView.frame.origin.x - frontImageView.frame.size.width - 40, 30)];
    [vechileBrandView addSubview:[self titleAndContent:@"车辆品牌:" andContent:@"广汽日野" withToAddress:nil]];
    [cell.contentView addSubview:vechileBrandView];
    
    // 运输介质
    UIView *mediumView = [[UIView alloc] initWithFrame:CGRectMake(frontImageView.frame.origin.x + frontImageView.frame.size.width - 10, vechileBrandView.frame.origin.y + vechileBrandView.frame.size.height, kUIScreenWidth - frontImageView.frame.origin.x - frontImageView.frame.size.width - 40, 30)];
    [mediumView addSubview:[self titleAndContent:@"运输介质:" andContent:@"甲醇" withToAddress:nil]];
    [cell.contentView addSubview:mediumView];
    
    // 罐体容积
    UIView *tankVolumeView = [[UIView alloc] initWithFrame:CGRectMake(frontImageView.frame.origin.x + frontImageView.frame.size.width - 10, mediumView.frame.origin.y + mediumView.frame.size.height, kUIScreenWidth - frontImageView.frame.origin.x - frontImageView.frame.size.width - 40, 30)];
    [tankVolumeView addSubview:[self titleAndContent:@"罐体容积:" andContent:@"25立方米" withToAddress:nil]];
    [cell.contentView addSubview:tankVolumeView];
    
    // 载重
    UIView *weightView = [[UIView alloc] initWithFrame:CGRectMake(frontImageView.frame.origin.x + frontImageView.frame.size.width - 10, tankVolumeView.frame.origin.y + tankVolumeView.frame.size.height, kUIScreenWidth - frontImageView.frame.origin.x - frontImageView.frame.size.width - 40, 30)];
    [weightView addSubview:[self titleAndContent:@"载        重:" andContent:@"20吨" withToAddress:nil]];
    [cell.contentView addSubview:weightView];
    
    // 推送货源按钮
    UIButton *pushShipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    pushShipButton.frame = CGRectMake(20, weightView.frame.origin.y + weightView.frame.size.height + 10, (kUIScreenWidth - 60) / 2, 40);
    pushShipButton.layer.masksToBounds = YES;
    pushShipButton.layer.cornerRadius = 5;
    pushShipButton.backgroundColor = [UIColor HexStrWithColor:@"AE0000"];
    [pushShipButton setTitle:@"推送货源" forState:UIControlStateNormal];
    [pushShipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [pushShipButton addTarget:self action:@selector(pushShipButtonAction:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:pushShipButton];
    
    // 电话联系按钮
    UIButton *connectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    connectionButton.frame = CGRectMake((kUIScreenWidth - 60) / 2 + 40, weightView.frame.origin.y + weightView.frame.size.height + 10, (kUIScreenWidth - 60) / 2, 40);
    connectionButton.layer.masksToBounds = YES;
    connectionButton.layer.cornerRadius = 5;
    connectionButton.backgroundColor = [UIColor HexStrWithColor:@"009100"];
    [connectionButton setTitle:@"拨打电话" forState:UIControlStateNormal];
    [connectionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [connectionButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:connectionButton];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kUIScreenHeight - 100;
}

#pragma mark 推送货源点击事件
- (void)pushShipButtonAction:(UIButton *)button{
    NSLog(@"推送货源");
}

#pragma mark 拨打电话按钮
- (void)phoneButtonAction:(UIButton *)button{
    NSLog(@"拨打电话");
    NSString *phoneNum = @"15146001603";
    
    UIWebView *callphonewebview;
    if (callphonewebview==nil) {
        callphonewebview=[[UIWebView alloc]init];
    }
    [self.view addSubview:callphonewebview];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNum]]];
    [callphonewebview loadRequest:request];
}


/**
 *  初始化列表
 */
- (void)initWithView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - 20) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}


/**
 *  自定义cell
 *
 *  @param title     标题
 *  @param content   内容
 *  @param toAddress 如果是地址显示到达的位置 如果该参数为nil自动设置为不显示
 *
 *  @return 生成一个标题和一个内容的view
 */
- (UIView *)titleAndContent:(NSString *)title andContent:(NSString *)content withToAddress:(NSString *)toAddress{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = content;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    contentLabel.font = [UIFont systemFontOfSize:15];
    // 如果标题为货单状态 改变详情颜色
    if ([title isEqualToString:@"货单状态:"]) {
        contentLabel.textColor = [UIColor HexStrWithColor:@"FF0000"];
    }
    //    if ([contentLabel.text isEqualToString:@"已确认"]) {
    //        contentLabel.textColor = [UIColor HexStrWithColor:@"8CEA00"];
    //    } else if ([contentLabel.text isEqualToString:@"未确认"]){
    //        contentLabel.textColor = [UIColor HexStrWithColor:@"FF2D2D"];
    //    }
    [bottomView addSubview:contentLabel];
    
    if ([title isEqualToString:@"路线:"]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width, contentLabel.frame.origin.y + 5, 18 / 2.0, 21 / 2.0)];
        imageView.image = [UIImage imageNamed:@"jt_xl"];
//        imageView.transform=CGAffineTransformMakeRotation(1.55);
        [bottomView addSubview:imageView];
        
        UILabel *toAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width, contentLabel.frame.origin.y , 200, 20)];
        toAddLabel.text = toAddress;
        [bottomView addSubview:toAddLabel];
    }
    
    return bottomView;
}

/**
 *  自定义cell
 *
 *  @param title            标题
 *  @param sendAndReceive   送货方和取货方
 *  @param isIdentification 是否被认证
 *  @param starSevel        星级等级
 *  @param phoneNumber      电话号
 *
 *  @return 生成一个标题，取货当送货方，星级，电话视图
 */
- (UIView *)twoAndStreeView:(NSString *)title sendAndReceive:(NSString *)sendAndReceive isIdentification:(BOOL)isIdentification starSevel:(float)starSevel phone:(NSString *)phoneNumber{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = sendAndReceive;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    [bottomView addSubview:contentLabel];
    contentLabel.font = [UIFont systemFontOfSize:15];
    contentLabel.textColor = [UIColor HexStrWithColor:@"CE0000"];
    // 是否认证
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width + 5, contentLabel.frame.origin.y + 5, 25, 15)];
    imageView.image = [UIImage imageNamed:@"yrz"];
    [bottomView addSubview:imageView];
    
    // 服务评价
    UILabel *evaluateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, titleLabel.frame.origin.y + titleLabel.frame.size.height + 10, 80, 20)];
    evaluateLabel.text = @"服务评价:";
    evaluateLabel.textAlignment = 2;
    [bottomView addSubview:evaluateLabel];
    
    // 星
    CWStarRateView *ratingControl = [[CWStarRateView alloc]initWithFrame:CGRectMake(evaluateLabel.frame.origin.x + evaluateLabel.frame.size.width + 20, evaluateLabel.frame.origin.y, 110, 20)];
    [bottomView addSubview:ratingControl];
    ratingControl.scorePercent = starSevel;
    ratingControl.hasAnimation = YES;
    
    // 拨打电话
    UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    phoneButton.frame = CGRectMake(kUIScreenWidth - 40, (50 - 30) / 2, 30, 30);
    [phoneButton setImage:[UIImage imageNamed:@"yrz"] forState:UIControlStateNormal];
    [phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:phoneButton];
    
    return bottomView;
}

/**
 *  自定义cell
 *
 *  @param title     标题
 *  @param starSevel 星级等级
 *
 *  @return 返回整个视图
 */
- (UIView *)fiveCellView:(NSString *)title withStar:(float)starSevel{
    
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    [bottomView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:15];
    
    // 星
    CWStarRateView *ratingControl = [[CWStarRateView alloc]initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, titleLabel.frame.origin.y, 110, 20)];
    [bottomView addSubview:ratingControl];
    ratingControl.scorePercent = starSevel;
    ratingControl.hasAnimation = YES;
    
    return bottomView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
